package com.nwpu.server.search.service.impl;

import com.nwpu.server.search.entity.NetworkRelationshipEntity;
import com.nwpu.server.search.service.NetworkRelationshipRepository;
import com.nwpu.server.search.service.NetworkRelationshipService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YuXingZh
 * @Date: Created in 16:25 2022-06-26
 * @Description:
 */
@Service
public class NetworkRelationshipServiceImpl implements NetworkRelationshipService {
    @Autowired
    private NetworkRelationshipRepository networkRelationshipRepository;

    @Override
    public Iterable<NetworkRelationshipEntity> getAll() {
        return networkRelationshipRepository.findAll();
    }

    @Override
    public List<NetworkRelationshipEntity> getByName(String name) {
        FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("description", "network");
        List<NetworkRelationshipEntity> list = new ArrayList<>();
        TermQueryBuilder matchQueryBuilder = new TermQueryBuilder("name", name);
        Iterable<NetworkRelationshipEntity> iterable = networkRelationshipRepository.search(fuzzyQuery);
        iterable.forEach(e->list.add(e));


        return list;
    }
}
